import pytest,requests
class Test_A3mall:
    token=None
    spid=None
    # 登录
    def test_login(self):
        url="http://10.59.9.12:7001/api/public/login"
        data={"username":"15727087537","password":"123456"}
        res=requests.post(url=url,json=data)
        print("登录接口",res.json())
        Test_A3mall.token="Bearer "+res.json()["data"]["token"]
        assert res.status_code==200
        assert "钻石会员" in res.text

    # 查看购物车
    def test_ck(self):
        url = "http://10.59.9.12:7001/api/cart?page=1"
        data = "page=1"
        header = {"auth-token": Test_A3mall.token, "content-type": "application/json"}
        res = requests.get(url=url, json=data, headers=header)
        print("查看购物车", res.json())
        Test_A3mall.spid = res.json()["data"]["list"][0]["id"]
        assert res.status_code == 200
        assert "音乐耳机" in res.text

    # 参数软布正确，确认成功
    def test_qr_001(self):
        url = f"http://10.59.9.12:7001/api/order/confirm?id={Test_A3mall.spid}&type=cart&shipping_type=1"
        header = {"auth-token": Test_A3mall.token, "content-type": "application/json"}
        res = requests.get(url=url, headers=header)
        print("确认订单", res.json())
        assert res.status_code == 200
        assert "音乐耳机" in res.text

    # 商品ID为空，确认失败
    def test_qr_002(self):
        url = f"http://10.59.9.12:7001/api/order/confirm?id={None}&type=cart&shipping_type=1"
        header = {"auth-token": Test_A3mall.token, "content-type": "application/json"}
        res = requests.get(url=url, headers=header)
        print("确认订单", res.json())
        assert res.status_code == 200
        assert "音乐耳机" in res.text

    # 商品ID为字符串类型，确认成功
    def test_qr_003(self):
        url = f"http://10.59.9.12:7001/api/order/confirm?id='{Test_A3mall.spid}'&type=cart&shipping_type=1"
        header = {"auth-token": Test_A3mall.token, "content-type": "application/json"}
        res = requests.get(url=url, headers=header)
        print("确认订单", res.json())
        assert res.status_code == 200
        assert "音乐耳机" in res.text

    # 商品ID为整形类型，确认成功
    def test_qr_004(self):
        url = f"http://10.59.9.12:7001/api/order/confirm?id={Test_A3mall.spid}&type=cart&shipping_type=1"
        header = {"auth-token": Test_A3mall.token, "content-type": "application/json"}
        res = requests.get(url=url, headers=header)
        print("确认订单", res.json())
        assert res.status_code == 200
        assert "音乐耳机" in res.text

    # 商品ID中月特殊符号，确认失败
    def test_qr_005(self):
        url=f"http://10.59.9.12:7001/api/order/confirm?id={Test_A3mall.spid}???&type=cart&shipping_type=1"
        header = {"auth-token": Test_A3mall.token, "content-type": "application/json"}
        res = requests.get(url=url, headers=header)
        print("确认订单",res.json())
        assert res.status_code == 200
        assert "音乐耳机" in res.text